package cn.huiyunche.admin.controller;

import java.math.BigDecimal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.huiyunche.base.service.enums.UserTypeEnum;
import cn.huiyunche.base.service.interfaces.BScoresService;
import cn.huiyunche.base.service.interfaces.CAccntBillService;
import cn.huiyunche.base.service.vo.CBillAdminVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;


/**
 * 账户账单管理
 * @author lm
 *
 */
@RequestMapping("/cbill")
@Controller
public class AccntBillController {
	
	private static final Logger log = LoggerFactory.getLogger(AccntBillController.class);
	
	@Autowired
	private CAccntBillService cAccntBillService = null;
	
	@Autowired
	private BScoresService bScoresService = null;
	
	private CAccntBillService getCAccntBillService(){
		return this.cAccntBillService;
	}
	
	private BScoresService getBScoresService() {
		return this.bScoresService;
	}
	
	/**
	 * 司机账单
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/driver", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> getDriverBills(PageVo page){
		log.info("select driver bills controller.");
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		CBillAdminVo bill = new CBillAdminVo();
		bill.setUserType(UserTypeEnum.DRIVER.getValue());
		result.setData(this.getCAccntBillService().getBills(page, bill, UserTypeEnum.DRIVER.getValue()));
		return result;
	}
	
	/**
	 * 承运公司账单
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/stancompany", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> getStanCompanyBills(PageVo page){
		log.info("select stancompany bills controller.");
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		CBillAdminVo bill = new CBillAdminVo();
		bill.setUserType(UserTypeEnum.COMPANY.getValue());
		result.setData(this.getCAccntBillService().getBills(page, bill, UserTypeEnum.COMPANY.getValue()));
		return result;
	}
	
	/**
	 * 托运个人账单
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/kyleowner", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> getOwnerBills(PageVo page){
		log.info("select kyleowner bills controller.");
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		CBillAdminVo bill = new CBillAdminVo();
		bill.setUserType(UserTypeEnum.OWNER.getValue());
		result.setData(this.getCAccntBillService().getBills(page, bill, UserTypeEnum.OWNER.getValue()));
		return result;
	}
	
	/**
	 * 托运公司账单
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/kylecompany", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> getKyleCompanyBills(PageVo page){
		log.info("select kylecompany bills controller.");
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		CBillAdminVo bill = new CBillAdminVo();
		bill.setUserType(UserTypeEnum.SCOMPANY.getValue());
		result.setData(this.getCAccntBillService().getBills(page, bill, UserTypeEnum.SCOMPANY.getValue()));
		return result;
	}
	
	/**
	 * 托运公司结算
	 * 
	 * @param orderId
	 * @param userId
	 * @param userType
	 * @param cost
	 * @return
	 */
	@RequestMapping(value = "/statement", method = RequestMethod.POST)
	@ResponseBody
	public Result<Object> statement(Long orderId, Long userId, int userType, BigDecimal cost){
		log.info("select kylecompany bills controller.");
		Result<Object> result = new Result<Object>(true, null, "数据处理成功");
		int res = this.getBScoresService().addTotalCost(orderId, userId, userType, cost);
		// 更新账单状态
		this.getCAccntBillService().updateBillStatus(orderId);
		result.setData(res);
		return result;
	}
	
	
	/**
	 * 运输公司结算
	 * 
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = "/stancompany", method = RequestMethod.POST)
	@ResponseBody
	public Result<Object> stanCompany(Long orderId){
		log.info("select kylecompany bills controller.");
		Result<Object> result = new Result<Object>(true, null, "数据处理成功");
		result.setData(this.getCAccntBillService().updateBillStatus(orderId));
		return result;
	}
	
}
